package chixing.day16.homework.work1;


import java.io.*;

/**
 * 1.拷贝一个文件，从一个目录到另外一个目录下（测试文本文件与图片文件）
 */
public class Copy {
    public static void main(String[] args) {
        fun1();
    }
    public static void fun1(){
        //1.将要复制的内容一个字节一个字节读取
        InputStream inputStream = null;
        try {
//            inputStream = new FileInputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\byte_hello.txt");
            inputStream = new FileInputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\Temp\\hsj.jpg");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        byte[] bytes = null;
        try {
            bytes = new byte[inputStream.available()];
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int temp;
        int i = 0;
        try {
            while ((temp = inputStream.read()) != -1){
                bytes[i++] = (byte) temp;
            }
        }catch (IOException e){
            e.printStackTrace();
        }//已经将读取的内容放到bytes中

        //2.在新目录下创建一个一样的文件名称
        inputStream.getClass().getName();
        //3.写入
        FileOutputStream fileOutputStream = null;
        try {
//            fileOutputStream = new FileOutputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\note\\byte_hello.txt");
            fileOutputStream = new FileOutputStream("E:\\2025-cx\\project\\PJ\\J01\\Javase\\note\\hsj.jpg");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        try {
            fileOutputStream.write(bytes);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        //4.关闭输入输出流
        if (fileOutputStream != null && inputStream != null){
            try {
                fileOutputStream.close();
                inputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
